package cmdb_account_management_v1

import (
	"context"

	cmdb_account_management_rpc "dfx_cmdb/cmdb-account-management-rpc/types/cmdb-account-management-rpc"
	"dfx_cmdb/cmdb-gateway/internal/svc"
	"dfx_cmdb/cmdb-gateway/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type UpdateAccountInfoLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewUpdateAccountInfoLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateAccountInfoLogic {
	return &UpdateAccountInfoLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *UpdateAccountInfoLogic) UpdateAccountInfo(req *types.UpdateAccountInfoRequest) (resp *types.PublicResponse, err error) {
	// 转换 API 请求为 RPC 请求
	grpcReq := &cmdb_account_management_rpc.UpdateAccessInfoRequest{
		Id:           req.Id,
		Type:         req.Type,
		Platform:     req.Platform,
		PlatformName: req.PlatformName,
		Account:      req.Account,
		Password:     req.Password,
		Token:        req.Token,
		Accesskey:    req.Accesskey,
		Accesssecret: req.Accesssecret,
		Comment:      req.Comment,
		OtherInfo:    req.OtherInfo,
	}

	// 调用 RPC 服务
	grpcResp, err := l.svcCtx.CmdbAccountManagementRpc.UpdateAccessInfo(l.ctx, grpcReq)
	if err != nil {
		logx.Errorf("调用 RPC 服务失败: %v", err)
		return nil, err
	}

	return &types.PublicResponse{
		Message: grpcResp.Message,
	}, nil
}
